using Infrastructure.Attribute;
using Infrastructure.Extensions;
using JTZY.Model.Goods.Dto;
using JTZY.Model.Goods;
using JTZY.Repository;
using JTZY.Service.Goods.IGoodsService;
using JTZY.Model.Order;

namespace JTZY.Service.Goods
{
    /// <summary>
    /// 商品表Service业务层处理
    /// </summary>
    [AppService(ServiceType = typeof(ICorecmsgoodsService), ServiceLifetime = LifeTime.Transient)]
    public class CorecmsgoodsService : BaseService<Corecmsgoods>, ICorecmsgoodsService
    {
        /// <summary>
        /// 查询商品表列表
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        public PagedInfo<CorecmsgoodsDto> GetList(CorecmsgoodsQueryDto parm)
        {
            var predicate = QueryExp(parm);

            var response = Queryable()
                //.Includes(x => x.CorecmsproductsNav) //填充子对象
                .Where(predicate.ToExpression())
                .ToPage<Corecmsgoods, CorecmsgoodsDto>(parm);

            return response;
        }


        /// <summary>
        /// 获取详情
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public Corecmsgoods GetInfo(int Id)
        {
            var response = Queryable()
                .Includes(x => x.CorecmsproductsNav) //填充子对象
                .Where(x => x.Id == Id)
                .First();

            return response;
        }

        public List<Corecmsgoods> GetTreeList()
        {

            var response = Queryable()
                .Includes(x => x.CorecmsproductsNav).ToList();

            return response;
        }

        /// <summary>
        /// 添加商品表
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public Corecmsgoods AddCorecmsgoods(Corecmsgoods model)
        {
            return Context.InsertNav(model).Include(s1 => s1.CorecmsproductsNav).ExecuteReturnEntity();
        }

        /// <summary>
        /// 导出商品表
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        public PagedInfo<CorecmsgoodsDto> ExportList(CorecmsgoodsQueryDto parm)
        {
            var predicate = QueryExp(parm);

            var response = Queryable()
                .Where(predicate.ToExpression())
                .Select((it) => new CorecmsgoodsDto()
                {
                }, true)
                .ToPage(parm);

            return response;
        }

        /// <summary>
        /// 查询导出表达式
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        private static Expressionable<Corecmsgoods> QueryExp(CorecmsgoodsQueryDto parm)
        {
            var predicate = Expressionable.Create<Corecmsgoods>();

            predicate = predicate.AndIF(!string.IsNullOrEmpty(parm.Bn), it => it.Bn == parm.Bn);
            predicate = predicate.AndIF(parm.GoodsCategoryId != null, it => it.GoodsCategoryId == parm.GoodsCategoryId);
            return predicate;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="productId"></param>
        /// <param name="userId"></param>
        /// <param name="v"></param>
        /// <param name="nums"></param>
        /// <returns></returns>
        public WebApiCallBack ChangeUserStock(int productsId, long userId, string type, int nums) 
        {
            var res = new WebApiCallBack() { methodDescription = "库存改变机制" };

            if (productsId == 0)
            {
                res.msg = "货品ID不能为空";
                return res;
            }
            var productModel = Context.Queryable<Corecmsuserstock>().Where(us => us.UserId == userId && us.ProductId == productsId).First();
            var bl = false;
            switch (type)
            {
                case "order": //下单
                    //更新订单购买量 -- 发货时处理
                    //DbClient.Updateable<CoreCmsGoods>().SetColumns(it => it.buyCount == it.buyCount + num).Where(p => p.id == productModel.goodsId).ExecuteCommand();
                    //判断是否有足够量去处理冻结库存变化

                    var insertNum = productModel.Stock < productModel.FreezeStock ? 0 : productModel.Stock - productModel.FreezeStock;
                    //更新记录。
                    bl = Context.Updateable<Corecmsuserstock>()
                        .SetColumns(it => it.FreezeStock == it.FreezeStock + nums )
                        .Where(p => p.Id == productModel.Id && insertNum >= nums && p.FreezeStock < p.Stock).ExecuteCommandHasChange();

                    break;
                case "cancel": //取消订单
                    bl = Context.Updateable<Corecmsuserstock>()
                        .SetColumns(it => it.FreezeStock == it.FreezeStock - nums)
                        .Where(p => p.Id == productModel.Id).ExecuteCommandHasChange();
                    break;
                default:
                    bl = Context.Updateable<Corecmsuserstock>()
                        .SetColumns(it => it.FreezeStock == it.FreezeStock + nums)
                        .Where(p => p.Id == productModel.Id).ExecuteCommandHasChange();
                    break;
            }
            res.status = bl;
            res.msg = bl ? "库存更新成功" : "库存不足";

            return res;
        }

    }
}